#!/usr/bin/perl

use strict;
use Getopt::Long;
use POSIX;

my %gv_opts;

my $IMAGEDIR = "/cm/images";

sub check_instance
# check whether there are any instance running for that user
{
    my @nodes = @_;
    
    my $firstnode = $nodes[0];

    # for stop instance
    run_cmd("su -c \"ssh $firstnode sqcheck 2>&1\" $gv_opts{squser} 2>&1", 1);

    check_remove("/opt/hp/$gv_opts{squser}");
    for my $node (@nodes) {
        # for remove instance
        check_remove("$node:/opt/hp/$gv_opts{squser}");

        # for umount /database/* /databasem/*
        # question: does it means we need to stop all the instance 
        #           even for other users (shared) ?
        run_cmd("ssh $node mount |grep database", 1); 
    }
}

sub check_mpts
# check whether thera are any mountpoints 
{
    my @nodes = @_;
    
    for my $node (@nodes) {
        check_remove("$node:/database/$gv_opts{squser}");
        check_remove("$node:/databasem/$gv_opts{squser}");
        check_remove("$node:/database/n*u*/$gv_opts{squser}");
        check_remove("$node:/databasem/n*u*/$gv_opts{squser}");
    }
}

sub check_fdisk
{
    for my $devline (`lsscsi|egrep '\\\[0' | egrep -v '0:0:[0-9]+:[6-9][0-9]'`) {
        chomp $devline;
        my @fields = split(" ", $devline);
        my $dev = $fields[-1];
        run_cmd("fdisk -l $dev |grep ^${dev}1", 1);
    }
}

sub check_exclude
{
    my $cmd;
    for my $category (get_categories()) {
        $cmd = "cmsh -c \"category; use $category; get excludelistupdate\""
             . "|grep $gv_opts{squser}";

        run_cmd($cmd, 1);

        $cmd = "cmsh -c \"category; use $category; get excludelistgrab\""
             . "|grep $gv_opts{squser}";
        run_cmd($cmd, 1);
    }
}

sub check_images
{
    for my $image (get_images()) {
        check_remove("$IMAGEDIR/$image/database/$gv_opts{squser}");
        check_remove("$IMAGEDIR/$image/databasem/$gv_opts{squser}");
        check_remove("$IMAGEDIR/$image/etc/rc.local.ORIG");
        check_remove("$IMAGEDIR/$image/etc/rc.local.nv");
    }
}

sub check_statusfile
{
    #not needed, sunce install_rpm will overwrite all the status
}

sub check_remove
{
    my ($node, $file) = split /:/, shift;
    
    ($node, $file) = ($file, $node) if (!defined($file));
    
    if (!defined($node)) {
        die "$file not removed from "  . caller() . "\n" if (-e $file);
    }
    else {
        run_cmd("ssh $node find $file -maxdepth 0 2>&1", 1);
    }
}

sub run_cmd($;$)
{
    my $cmd = shift;
    my $checkNoZero = shift; # 
    
    $checkNoZero = 0 if (!defined($checkNoZero));
    
    print "Run $cmd\n";

    my $output = `$cmd`;
    
    if ($checkNoZero) { # 
       die("Check failed for '$cmd' from " . caller() . "\n") 
           if (WEXITSTATUS($?) == 0);  
    } 
    else {
       die("Check failed for '$cmd' from " . caller() . "\n") 
           if (WEXITSTATUS($?) != 0);
    }
}

sub get_images
{   
    my @images;
    chomp(@images = `ls $IMAGEDIR 2>/dev/null`);

    return wantarray ? @images : join(',', @images);
}

sub get_categories
{
    my @categories;
    @categories = ("edge");

    return @categories;
}

sub main
{
    my @noderange = split /-/, $gv_opts{noderange};
    
    my @nodes = ($noderange[0] .. $noderange[1]);
    
    check_instance(@nodes);
    check_mpts(@nodes);
    
    check_fdisk();
    
    run_cmd("mount | grep /designs/seaquest", 1); # umount of /designs/

    check_exclude();
    check_images();

    check_remove("/home/dont_delete_me");
    check_remove("/home/$gv_opts{squser}");
    check_remove("/home/pmtext/$gv_opts{squser}");
    check_remove("/home/sealog/$gv_opts{squser}");
    check_remove("/etc/fstab_PREINSTCONFIG");
    #$cmd = "grep -w $gv_opts{squser} /home/lunmgr/lunmgr.devtable";
    check_remove("/home/SQFQA/temp/$gv_opts{squser}");
    check_remove("/opt/hp/nv/release/install_$gv_opts{squser}_logs");
    check_remove("/opt/hp/nv/release/release-$gv_opts{squser}");

    check_statusfile();
}

GetOptions(\%gv_opts,
            "squser=s",
            "noderange=s"
            );
            
die "No enough options\n" 
    unless (defined($gv_opts{squser}) && defined($gv_opts{noderange}));
main();
